/*
 * Copyright (c) 2006-2025, RT-Thread Development Team
 *
 * SPDX-License-Identifier: Apache-2.0
 *
 * Change Logs:
 * Date           Author       Notes
 * 2025-06-23     RT-Thread    first version
 */

#include <rtthread.h>

#define DBG_TAG "main"
#define DBG_LVL DBG_LOG
#include <rtdbg.h>

#include <rtdevice.h>
#include <board.h>

#define LED_PIN GET_PIN(D, 1)

#include "main.h"
#include "tim.h"
#include "usart.h"
#include "gpio.h"

#include "motor.h"
#include "pid.h"
#include "Bluetooth.h"
#include "MG90.h"
#include "run.h"
uint8_t g_ucUsart3ReceiveData;
uint8_t Usart1ReceiveData;
int16_t data[10];
int16_t PI_data[2];
extern float max;
extern float min;

extern float Motor1Speed;
extern float Motor2Speed;
extern float Motor3Speed;
extern float Motor4Speed;

extern int motor_pwm1;
extern int motor_pwm2;
extern int motor_pwm3;
extern int motor_pwm4;
void SystemClock_Config(void);

static rt_thread_t thread_a = RT_NULL;

static void thread_entry(void *parameter){
    int count = 1;
    //rt_pin_mode(LED_PIN, PIN_MODE_OUTPUT);
    HAL_Init();
    SystemClock_Config();
    MX_GPIO_Init();
    MX_TIM1_Init();
    MX_TIM2_Init();
    MX_TIM3_Init();
    MX_TIM4_Init();
    MX_TIM5_Init();
    MX_TIM6_Init();
    MX_USART3_UART_Init();
    MX_TIM9_Init();
    MX_USART1_UART_Init();
    HAL_TIM_Base_Start_IT(&htim1);
    HAL_TIM_Base_Start_IT(&htim2);
    HAL_TIM_Base_Start_IT(&htim3);
    HAL_TIM_Base_Start_IT(&htim4);
    HAL_TIM_Base_Start_IT(&htim5);
    __HAL_UART_ENABLE_IT(&huart3,UART_IT_RXNE);
    __HAL_UART_ENABLE_IT(&huart1,UART_IT_RXNE);
    car_init();
    pid_init();
    bluetooth_init();
    UART1_init();
    mg90_init();

    LOG_D("Hello RT-Thread!");
    while(1){
        run_Proc();
        /*
        LOG_D("Hello RT-Thread!");
        rt_pin_write(LED_PIN, PIN_HIGH);
        rt_thread_mdelay(1000);
        rt_pin_write(LED_PIN, PIN_LOW);
        rt_thread_mdelay(1000);
        */

    }

}
int main(void)
{
    int count = 1;


    //创建线程
    thread_a = rt_thread_create("thread_entry",thread_entry, RT_NULL, 512, 25, 10);
    //是否创建成功
    if(thread_a != RT_NULL){
        //启动线程
        rt_thread_startup(thread_a);
    }else{
        LOG_D("Failed to create LED thread!");
    }


    while (count++)
    {

        rt_thread_mdelay(1000);
    }

    return RT_EOK;
}

